﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BepInEx;
using System.Reflection;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
using Blis.Common;
using Blis.Common.Utils;
using Blis.Client;
using Blis.Client.UI;
using System.Threading;
using System.Collections;

namespace BlackSurvivalCheat
{
    [BepInPlugin("BlackSurvivalCheat", "BlackSurvivalCheat", "1.0.0.0")]
    public class CheatMain : BaseUnityPlugin
    {
        public static ConfigEntry<bool> sum_flag;
        private static ConfigEntry<KeyboardShortcut> start_earn { get; set; }
        public static ConfigEntry<KeyboardShortcut> pick_up { get; set; }

        // Awake is called once when both the game and the plug-in are loaded
        void Awake()
        {
            Logger.LogInfo("Mengluu Awake");
            start_earn = Config.Bind("Hotkeys", "获取材料", new KeyboardShortcut(KeyCode.F4));
            pick_up = Config.Bind("Hotkeys", "捡垃圾", new KeyboardShortcut(KeyCode.F5));
            sum_flag = Config.Bind("Hotkeys", "is_open", true, "全部功能总开关");

            var harmony = new Harmony("Cheat_For_Mengluu");

            X_Ray.Load(harmony);
            HookFocusUI.Load(harmony);
            PickUpRubish.Load(harmony);
            HookCreatePacket.Load(harmony);

        }

        void Update()
        {
            GameClient gameClient = MonoBehaviourInstance<GameClient>.inst;
            ClientService clientService = MonoBehaviourInstance<ClientService>.inst;
            if (start_earn.Value.IsDown() && HookFocusUI.itemcode != 0)
            {
                gameClient.Request(new ReqAdminCreateItem
                {
                    itemCode = HookFocusUI.itemcode
                }, NetChannel.ReliableOrdered);
            }
            if (pick_up.Value.IsDown() && clientService != null)
            {
                StartCoroutine(StartPickUp());
                /*List<LocalObject> objects = clientService.World.FindAll<LocalObject>();
                foreach (LocalObject local in objects)
                {
                    if(local.ObjectType== ObjectType.ItemBox|| local.ObjectType == ObjectType.StaticItemBox || local.ObjectType == ObjectType.ResourceItemBox || local.ObjectType == ObjectType.AirSupplyItemBox)
                    {
                        gameClient.Request(new ReqOpenItemBox
                        {
                            targetId = local.ObjectId,
                            interactionBoxId = local.ObjectId
                        }, NetChannel.ReliableOrdered);
                        gameClient.Request(new ReqCheat
                        {
                            cheatCommand = "HpSpFull"
                        }, NetChannel.ReliableOrdered);
                    }
                }*/
            }
        }
        IEnumerator StartPickUp()
        {
            
            ClientService clientService = MonoBehaviourInstance<ClientService>.inst;
            GameClient gameClient = MonoBehaviourInstance<GameClient>.inst;
            List<LocalObject> objects = clientService.World.FindAll<LocalObject>();
            gameClient.Request(new ReqCheat
            {
                cheatCommand = "StopAreaRestriction"
            }, NetChannel.ReliableOrdered);
            foreach (LocalObject local in objects)
            {
                if (local.ObjectType == ObjectType.ItemBox || local.ObjectType == ObjectType.StaticItemBox || local.ObjectType == ObjectType.ResourceItemBox || local.ObjectType == ObjectType.AirSupplyItemBox)
                {
                    gameClient.Request(new ReqOpenItemBox
                    {
                        targetId = local.ObjectId,
                        interactionBoxId = local.ObjectId
                    }, NetChannel.ReliableOrdered);
                    gameClient.Request(new ReqCheat
                    {
                        cheatCommand = "HpSpFull"
                    }, NetChannel.ReliableOrdered);
                }
                gameClient.lastPingUpdateTick = Time.time;
                yield return new WaitForSeconds(1f);
            }
        }
    }
}
